package com.chinasoft.set01.map;

import java.util.HashMap;
import java.util.Map;

public class Demo02 {
    public static void main(String[] args) {

        Map<String,String> map = new HashMap<>();
        //put(K key, V value)
        map.put("k1","v1");
        map.put("k2","v2");
        map.put("k3","v3");
        map.put("k4","v4");
        map.put("k5","v5");
        //method1(map);
        //method2(map);
        //method3(map);
        //method4(map);
        //method5(map);
        //method6(map);
        //method7(map);
    }

    private static void method7(Map<String, String> map) {
        //集合的长度
        int size = map.size();
        System.out.println("size = " + size);
    }

    private static void method6(Map<String, String> map) {
        //判断集合是否为空
        boolean b1 = map.isEmpty();
        System.out.println("b1 = " + b1);
        map.clear();
        boolean b2 = map.isEmpty();
        System.out.println("b2 = " + b2);
    }

    private static void method5(Map<String, String> map) {
        //判断集合是否包含指定的值
        boolean b1 = map.containsValue("v1");
        System.out.println("b1 = " + b1);
        boolean b8 = map.containsValue("v8");
        System.out.println("b8 = " + b8);
    }

    private static void method4(Map<String, String> map) {
        //判断集合是否包含指定的键
        boolean b2 = map.containsKey("k2");
        boolean b8 = map.containsKey("k8");
        System.out.println("b2 = " + b2);
        System.out.println("b8 = " + b8);
    }

    private static void method3(Map<String, String> map) {
        //移除全部键值对
        map.clear();
        System.out.println("map = " + map);
    }

    private static void method2(Map<String, String> map) {
        //根据键删除值
        String s = map.remove("k1");
        System.out.println("s = " + s);
        System.out.println(map);
    }

    private static void method1(Map<String, String> map) {
        //如果要添加的键不存在，那会会把键值都添加到集合中
        //如果要添加的键存在，那么会覆盖原先的值，把原先的值作为返回值进行返回
        String s = map.put("k5","v6");
        System.out.println(s);
        System.out.println(map);
    }
}